package cn.pb.service.imp;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.pb.dao.UserDao;
import cn.pb.domin.User;
import cn.pb.exception.UserExistExeception;
import cn.pb.exception.UserUnactiveException;
import cn.pb.service.UserService;
import cn.pb.utils.MD5Utils;

/**
 * @ClassName UserServiceImp类
 * @Author whw
 * @Date 2017年9月6日 上午11:09:08
 * @Version 1.0
 * @Description
 */
@Transactional // 业务层事务管理
@Service
public class UserServiceImp implements UserService {
	// dao层对象
	@Autowired
	private UserDao ud;

	@Override
	public User isUserExist(String uemail) {
		User user = ud.findUserByUemail(uemail);
		return user;
	}

	@Override
	public void register(User user) throws UserExistExeception {
		if (isUserExist(user.getUemail()) != null) {
			throw new UserExistExeception();
		}
		try {
			user.setUpsw(MD5Utils.getMd5(user.getUpsw()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		ud.add(user);
	}

	@Override
	public boolean active(String uemail) {
		Integer count = ud.updateByEmail(uemail);
		return count == 1;
	}

	@Override
	public User login(User user) {
		try {
			user.setUpsw(MD5Utils.getMd5(user.getUpsw()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		User usertest = ud.findByUserNameandPsw(user);
		if (usertest != null && usertest.getUactive().equals("false")) {
			throw new UserUnactiveException("用户未激活！");
		}
		return usertest;
	}

}
